package com.lungle.warmdiary.app.view.active.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.model.response.MediaAccount;
import com.lungle.warmdiary.app.model.response.MediaAccountList;
import com.lungle.warmdiary.app.model.response.MediaChannel;
import com.lungle.warmdiary.app.model.response.MediaChannelList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by God on 2016/8/11.
 */
public class MediaSelectAdapter extends BaseAdapter {

    private MediaChannelList mMediaChannelList;
    private Context mContext;
    private onAccountSelected mOnAccountSelected;


    public MediaSelectAdapter(Context context, onAccountSelected selected, MediaChannelList mediaChannelList) {
        mContext = context;
        mMediaChannelList = mediaChannelList;
        this.mOnAccountSelected = selected;
    }

    @Override
    public int getCount() {
        return mMediaChannelList.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null || convertView.getTag() == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.list_cell_popup_active_header, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        MediaChannel channel = mMediaChannelList.getChannelList().get(position);
        if (channel.getPlatform() == MediaChannel.PLATFORM_WECHAT) {
            vh.mTvChannelTitle.setText("微信朋友圈");
            Resources res = mContext.getResources();
            Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_wechat_friend);
            vh.mTvChannelTitle.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(res, bitmap), null, null, null);
            vh.mTvChannelTitle.setPadding(0, 0, 0, 0);
            vh.mTvChannelTitle.setTextColor(Color.parseColor("#4FB327"));
        }

        vh.mAgvMediaAccountContainer.setAdapter(new InnerAdapter(mContext, mOnAccountSelected, channel.getAccounList()));
        setListViewHeightBasedOnChildren(vh.mAgvMediaAccountContainer);

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.media_channel)
        TextView mTvChannelTitle;
        @Bind(R.id.agv_media_account_container)
        ListView mAgvMediaAccountContainer;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class InnerAdapter extends BaseAdapter {
        private MediaAccountList mMediaAccountList;

        private Context mContext;
        private onAccountSelected mAccountSelected;

        public InnerAdapter(Context context, onAccountSelected selected, MediaAccountList mediaAccountList) {
            mMediaAccountList = mediaAccountList;
            mContext = context;
            this.mAccountSelected = selected;
        }

        @Override
        public int getCount() {
            int count;
            if (mMediaAccountList.getAccountList().size() % 2 == 0) {
                count = mMediaAccountList.getAccountList().size() / 2;
            } else {
                count = mMediaAccountList.getAccountList().size() / 2 + 1;
            }

            return count;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            InnerViewHolder vh;
            if (convertView == null || convertView.getTag() == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(
                        R.layout.view_media_account, parent, false);
                vh = new InnerViewHolder(convertView);

            } else {
                vh = (InnerViewHolder) convertView.getTag();
            }
            MediaAccount firstItem = (MediaAccount) mMediaAccountList.getList().get(position * 2);
            vh.mTvAccountNameFirst.setText(firstItem.getName());
            vh.mTvAccountNameFirst.setOnClickListener(v -> mAccountSelected.onAccountSelected(firstItem));
            if ((position * 2 + 1) < (mMediaAccountList.getAccountList().size())) {
                MediaAccount secondItem = (MediaAccount) mMediaAccountList.getList().get(position * 2 + 1);
                vh.mTvAccountNameSecond.setText(secondItem.getName());
                vh.mTvAccountNameSecond.setOnClickListener(v -> mAccountSelected.onAccountSelected(secondItem));
            }


            return convertView;
        }


    }

    static class InnerViewHolder {
        @Bind(R.id.account_name_first)
        TextView mTvAccountNameFirst;
        @Bind(R.id.account_name_second)
        TextView mTvAccountNameSecond;

        InnerViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface onAccountSelected {
        void onAccountSelected(MediaAccount mediaAccount);
    }


    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

